<?php
/**
 * Created by PhpStorm.
 * User: chenyuz
 * Date: 2019/3/5
 * Time: 15:41
 */
namespace app\ops\controller;

use think\Db;

class Award extends Base{

    public function index() {

        $list = Db::name("award")->order("status, award_id desc")->paginate(10)->each(function (&$item){
            if($item['type'] == 1)
            {
                $item['condition'] = date('Y-m-d H:i:s', strtotime($item['create_time']) + 86400 * $item['condition']);
            }
            return $item;
        });
        $this->assign("list", $list);
        return $this->fetch();
    }

    function edit($id = 0)
    {
        $post = request()->post();
        if($post)
        {
            unset($post['image']);
            if($post['award_id'])
            {
                db('award')->update($post);
            }else{
                unset($post['award_id']);
                db('award')->insert($post);
            }
            return success([]);
        }else{
            $data = db('award')->where(['award_id'=> $id])->find();
//            dump($data);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }


    function delete()
    {
        $id = request()->post('id');
        db('award')->where(['award_id'=> $id])->delete();
        return success([]);
    }

    function edit2($id = 0)
    {
        $post = request()->post();
        if($post)
        {
            if($post['reward_id'])
            {
                db('reward')->update($post);
            }else{
                unset($post['reward_id']);
                db('reward')->insert($post);
            }
            return success([]);
        }else{
            $data = db('reward')->where(['reward_id'=> $id])->find();
//            dump($data);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }


    function delete2()
    {
        $id = request()->post('id');
        db('reward')->where(['reward_id'=> $id])->delete();
        return success([]);
    }

    function upload()
    {
        $file = request()->file('imgFile');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->validate(['size'=>2097152])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                // 成功上传后 获取上传信息
                // 输出 jpg
                $url = DS . 'uploads' . DS . $info->getSaveName();
            } else {
                // 上传失败获取错误信息
                return json_encode(['error'=> 1, 'message'=> '上传失败']);
            }
        } else {
            return fail();
        }

        return json_encode(['error'=> 0, 'url' => config('host') . $url]);

    }

    public function reward() {

        $list = Db::name("reward")->alias('r')
            ->join('coupon c', 'c.coupon_id = r.value')
            ->field('r.*, c.name as coupon_name')
            ->order("r.reward_id desc")
            ->paginate(10);
        $this->assign("list", $list);
        return $this->fetch();
    }


}